//
//  SAWCorrectHomeWorkVC.m
//  BestLearning
//
//  Created by edz on 2018/1/31.
//  Copyright © 2018年 apple. All rights reserved.
//
#import "SAWAllCorrectHomework.h"
#import "SAWAllKnowlageVC.h"
#import "CorrectModel.h"
#import "SAWCorrectModel.h"
#import "SAWRefreTableView.h"
#import "SAWCorrectHomeWorkVC.h"
#import "LLNavigationMidSearchItem.h"
#import "IWTitleButton.h"
@interface SAWCorrectHomeWorkVC ()<UITextFieldDelegate>
@property (nonatomic,strong) UIView *allViews;
@property (nonatomic,strong) UITextField *searchTextField;
@property (nonatomic,strong) UIButton *searchButton;


@property(nonatomic,strong)NSMutableArray *dataItemArr;
@property (nonatomic,assign) NSInteger pageNum;
@property (nonatomic,assign)BOOL isUpRefresh;
@property(nonatomic,strong)SAWCorrectModel *model;
@property(nonatomic,strong)SAWRefreTableView *tableView;
@end

@implementation SAWCorrectHomeWorkVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setSearchName];
   
 self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTargat:self action:@selector(popHomeView) image:@"icon_back_default1" highImage:nil];
    [self setUI];
}
-(SAWCorrectModel *)model
{
    if (!_model) {
        _model = [[SAWCorrectModel alloc]init];
    }
    return _model;
}
-(NSMutableArray *)dataItemArr
{
    if (!_dataItemArr) {
        _dataItemArr = [[NSMutableArray alloc]init];
    }
    return _dataItemArr;
}
-(void)setUI
{
    _tableView = [[SAWRefreTableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-kTopHeight)];
     __weak typeof(self) weakSelf = self;
    _tableView.pushVC = ^(NSString *ID, BOOL WH) {
        if (WH) {
            SAWAllCorrectHomework *vc = [SAWAllCorrectHomework new];
            vc.ID = ID;
            [weakSelf.navigationController pushViewController:vc animated:YES];
        }
        else
        {
            SAWAllKnowlageVC*vc = [SAWAllKnowlageVC new];
            vc.ID = ID;
        [weakSelf.navigationController pushViewController:vc animated:YES];
        }
        
    };
    [self.view addSubview:_tableView];
    //上拉加载
    _tableView.SAWTabView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(upRefresh)];
    //下拉刷新
    _tableView.SAWTabView.mj_header  = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(downRefresh)];
    
    _pageNum = 1;
    [self loadData];
    

   
   
    
}
-(void)loadData
{
    [self.model dataWithOrder:^(NSArray *dataArr) {
        
        if (_isUpRefresh) {
           [self.dataItemArr removeAllObjects];
        }
       
        
        for (NSDictionary *dic in dataArr) {
            CorrectModel *model = [CorrectModel mj_objectWithKeyValues:dic];
            model.ID = [dic objectForKey:@"id"];
            [self.dataItemArr addObject:model];
        }
        _tableView.SAWDataArr = self.dataItemArr;
        
    } withpage:_pageNum withSearchName:nil withFild:^{
        [_tableView.SAWTabView.mj_footer endRefreshing];
        [_tableView.SAWTabView.mj_header endRefreshing];
    }];
}
-(void)upRefresh
{
    _pageNum ++;
    _isUpRefresh=NO;
    [self loadData];
    
}
-(void)downRefresh
{
    _pageNum = 1;
    _isUpRefresh=YES;
    [self loadData];
    
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //1.显示状态栏
    [UIApplication sharedApplication].statusBarHidden = NO;
    //设置全局状态栏字体颜色为黑色
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    [self.navigationController.navigationBar setBackgroundImage:[LLTools createImageWithColor:[UIColor whiteColor]] forBarMetrics:(UIBarMetricsDefault)];//设置导航栏颜色
    
    
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]}];
    
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    // [UIApplication sharedApplication].statusBarHidden = YES;
    //NavigationBar背景色
//    [self.navigationController.navigationBar setBarTintColor:UIColor(139, 198, 253)];
//    [self.navigationController.navigationBar setBackgroundImage:[LLTools createImageWithColor:UIColor(139, 198, 253)] forBarMetrics:(UIBarMetricsDefault)];//设置
}

#pragma mark ---初始化导航栏

- (void)setSearchName{
    
    self.allViews = [[UIView alloc]initWithFrame:CGRectMake(40, 0, kScreenWidth - 180, 30)];
    self.allViews.backgroundColor = ColorRGB(247,245,245);
    self.allViews.layer.cornerRadius = 15;
    self.allViews.clipsToBounds = YES;
    self.navigationItem.titleView = self.allViews;
    
    self.searchTextField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, self.allViews.frame.size.width, self.allViews.frame.size.height)];
    self.searchTextField.font = [UIFont systemFontOfSize:13];
    self.searchTextField.textColor = ColorRGB(102, 102, 102);
    self.searchTextField.textAlignment = NSTextAlignmentCenter;
    self.searchTextField.returnKeyType =  UIReturnKeySearch;
    self.searchTextField.clearButtonMode = UITextFieldViewModeAlways;
    self.searchTextField.delegate = self;
    [self.allViews addSubview:self.searchTextField];
    
    
    self.searchButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [self.searchButton setImage:[UIImage imageNamed:@"icon_serch_default"] forState:(UIControlStateNormal)];
    
    [self.searchButton setTitle:@"  按课程名称搜索" forState:(UIControlStateNormal)];
    [self.searchButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    self.searchButton.titleLabel.font = [UIFont systemFontOfSize:13];
    self.searchButton.frame = self.searchTextField.frame;
    [self.allViews addSubview:self.searchButton];
    
    [self.searchButton addTarget:self action:@selector(searchButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.searchTextField addTarget:self action:@selector(searchTextFieldAction:) forControlEvents:(UIControlEventEditingChanged)];
    
    
}


- (void)searchButtonAction:(UIButton *)sender{
    
    
    [self.searchTextField becomeFirstResponder];
    
}

- (void)searchTextFieldAction:(UITextField *)textField{
    
    
    self.searchButton.hidden = textField.text.length;
    
    
    
    
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [self.searchTextField resignFirstResponder];
    
    
    //[self.tableView.mj_header beginRefreshing];
    
    return YES;
}
-(void)popHomeView
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
